﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using AlmaDatabase;

namespace Alma.Models
{
    public class ListModel
    {
        public long ID { get; set; }
        public long ID_User { get; set; }
        public String List_Name { get; set; }
        public List<ProductDetailsModel> productList { get; set; }

        public ListModel()
        {
            productList = new List<ProductDetailsModel>();
        }

        public ListModel(long user)
        {
            ID_User = user;
        }

        public ListModel(long userID, string name)
        {
            ID_User = userID;
            List_Name = name;
        }

        public ListModel(long idList, long userID, string name)
        {
            ID = idList;
            ID_User = userID;
            List_Name = name;
            productList = new List<ProductDetailsModel>();
            using (var context = new AlmaDBEntities())
            {
                /*product*/
                var list = from product in context.Products
                           join jproduct in context.J_List_Products on product.ID equals jproduct.ID_Product
                           where jproduct.ID_List == idList
                           select product;
                foreach (var pro in list)
                {
                    ProductDetailsModel productModel = new ProductDetailsModel(pro);
                    var listProd = from cart in context.J_List_Products
                                   where cart.ID_Product == pro.ID
                                   select cart;
                    productModel.Quantity = listProd.First().Quantity;
                    productList.Add(productModel);
                }
            }
        }
    }
}